package com.ztesoft.web.common;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.ChannelShell;
import com.jcraft.jsch.JSchException;

public class PublicSSHExec {
	
	private static Log log=LogFactory.getLog(PublicSSHExec.class);
	/**
	 * 执行指定命令
	 * @param channel
	 * @param command
	 * @return
	 * @throws IOException
	 * @throws JSchException
	 * @throws InterruptedException 
	 */
	public Map<String, String> execNormalCommand(ChannelExec channel,String command) throws JSchException, InterruptedException, IOException{
		Map<String, String> result=new HashMap<String, String>();
		channel.setCommand(command);
		InputStream in=null;
		try {
			in=channel.getInputStream();
			channel.connect(120000);
		    byte[] tmp=new byte[1024];
		    StringBuffer buffer=new StringBuffer();
		    while(true){
		    	Thread.sleep(6000);
		    	while(in.available()>0){
		    		int i=in.read(tmp, 0, 1024);
		    		if(i<0)break;
		    		buffer.append(new String(tmp, 0, i));
		        }
		        if(channel.isClosed()){
		        	if(in.available()>0) continue;
		        	result.put("status", channel.getExitStatus()+"");
		        	break;
		        }else if(in.available()<=0){
		        	result.put("status", channel.getExitStatus()+"");
		        	break;
		        }
		    }
		    result.put("result", buffer.toString());
			return result;
		} catch (IOException e) {
			// TODO: handle exception
			log.error("执行命令时获取执行命令结果异常"+e);
			return result;
		}finally{
			if(in!=null){
				in.close();
			}
		}
		
	}
	
	/**
	 * 执行指定shell
	 * @param channel
	 * @param command
	 * @return
	 * @throws IOException
	 * @throws JSchException
	 * @throws InterruptedException 
	 */
	public Map<String, String> execShellCommand(ChannelShell channel,String command) throws JSchException, InterruptedException, IOException{
		Map<String, String> result=new HashMap<String, String>();
//		channel.setCommand(command);
		InputStream in=null;
		OutputStream outstream = null;
		try {
			outstream = channel.getOutputStream();
			in=channel.getInputStream();
			channel.connect(120000);
			outstream.write(command.getBytes());
			outstream.flush();
		    byte[] tmp=new byte[1024];
		    StringBuffer buffer=new StringBuffer();
		    while(true){
		    	Thread.sleep(6000);
		    	while(in.available()>0){
		    		int i=in.read(tmp, 0, 1024);
		    		if(i<0)break;
		    		buffer.append(new String(tmp, 0, i));
		        }
		        if(channel.isClosed()){
		        	if(in.available()>0) continue;
		        	result.put("status", channel.getExitStatus()+"");
		        	break;
		        }else if(in.available()<=0){
		        	result.put("status", channel.getExitStatus()+"");
		        	break;
		        }
		    }
		    result.put("result", buffer.toString());
			return result;
		} catch (IOException e) {
			// TODO: handle exception
			log.error("执行命令时获取执行命令结果异常"+e);
			return result;
		}finally{
			if(in!=null){
				in.close();
			}
			if(outstream!=null){
				outstream.close();
			}
		}
		
	}
}
